home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / PureImmediate / PureImmediate.java.z / PureImmediate.java
Encoding:
Java Source  |  2003-08-08  |  4.5 KB  |  145 lines

  1. /*
  2.  *    @(#)PureImmediate.java 1.19 02/10/21 13:53:05
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import java.applet.Applet;
  41. import java.awt.BorderLayout;
  42. import java.awt.GraphicsConfiguration;
  43. import java.awt.event.*;
  44. import com.sun.j3d.utils.applet.MainFrame;
  45. import com.sun.j3d.utils.geometry.ColorCube;
  46. import com.sun.j3d.utils.universe.*;
  47. import javax.media.j3d.*;
  48. import javax.vecmath.*;
  49.  
  50. /**
  51.  * Pure immediate mode example program.  In pure immediate mode, the
  52.  * renderer must be stopped on the Canvas being rendered into. In our
  53.  * example, this is done immediately after the canvas is created. A
  54.  * separate thread is started up to do the immediate mode rendering.
  55.  */
  56. public class PureImmediate extends Applet implements Runnable {
  57.  
  58.     private Canvas3D canvas;
  59.     private GraphicsContext3D gc = null;
  60.     private Geometry cube = null;
  61.     private Transform3D cmt = new Transform3D();
  62.  
  63.     // One rotation (2*PI radians) every 6 seconds
  64.     private Alpha rotAlpha = new Alpha(-1, 6000);
  65.  
  66.     private SimpleUniverse u = null;
  67.  
  68.     //
  69.     // Renders a single frame by clearing the canvas, drawing the
  70.     // geometry, and swapping the draw and display buffer.
  71.     //
  72.     public void render() {
  73.     if (gc == null) {
  74.         // Set up Graphics context
  75.         gc = canvas.getGraphicsContext3D();
  76.         gc.setAppearance(new Appearance());
  77.  
  78.         // Set up geometry
  79.         cube = new ColorCube(0.4).getGeometry();
  80.     }
  81.  
  82.     // Compute angle of rotation based on alpha value
  83.     double angle = rotAlpha.value() * 2.0*Math.PI;
  84.     cmt.rotY(angle);
  85.  
  86.     // Render the geometry for this frame
  87.     gc.clear();
  88.     gc.setModelTransform(cmt);
  89.     gc.draw(cube);
  90.     canvas.swap();
  91.     }
  92.  
  93.  
  94.     //
  95.     // Run method for our immediate mode rendering thread.
  96.     //
  97.     public void run() {
  98.     System.out.println("PureImmediate.run: starting main loop");
  99.     while (true) {
  100.         render();
  101.         Thread.yield();
  102.     }
  103.     }
  104.  
  105.  
  106.     public PureImmediate() {
  107.     }
  108.  
  109.     //
  110.     // init: create the canvas, stop the renderer,
  111.     // create the universe, and start the drawing thread.
  112.     //
  113.     public void init() {
  114.     setLayout(new BorderLayout());
  115.         GraphicsConfiguration config =
  116.            SimpleUniverse.getPreferredConfiguration();
  117.  
  118.         canvas = new Canvas3D(config);
  119.         canvas.stopRenderer();
  120.     add("Center", canvas);
  121.  
  122.     // Create the universe and viewing branch
  123.     u = new SimpleUniverse(canvas);
  124.  
  125.         // This will move the ViewPlatform back a bit so the
  126.         // objects in the scene can be viewed.
  127.         u.getViewingPlatform().setNominalViewingTransform();
  128.  
  129.     // Start a new thread that will continuously render
  130.     new Thread(this).start();
  131.     }
  132.  
  133.     public void destroy() {
  134.     u.cleanup();
  135.     }
  136.  
  137.     //
  138.     // The following allows PureImmediate to be run as an application
  139.     // as well as an applet
  140.     //
  141.     public static void main(String[] args) {
  142.     new MainFrame(new PureImmediate(), 256, 256);
  143.     }
  144. }
  145.